package com.huanzhidadi.single.collection;

import com.alibaba.fastjson2.JSON;

import java.util.ArrayList;

public class IteratorTest {
    public static void main(String[] args) {
        ArrayList<String> l1 = new ArrayList();
        l1.add("aaa");
        l1.add("bbb");
        l1.add("ccc");
        l1.add("ddd");
        l1.add("eee");
        System.out.println("初始化列表：" + JSON.toJSONString(l1));

        // 使用iterator.remove()删除集合中的元素
        /*Iterator<String> iterator = l1.iterator();
        while (iterator.hasNext()) {
            String temp = iterator.next();
            if ("ddd".equals(temp)) {
                iterator.remove();
            }
            System.out.println(temp);
        }*/

        /*for (String elem : l1) {
            if ("ddd".equals(elem) || "ccc".equals(elem)) {
                l1.remove(elem);
            }
            System.out.println(elem);
        }*/

        for (int i=0; i<l1.size(); i++) {
            System.out.println(i);
            l1.remove(i);
            if ("ddd".equals(l1.get(i)) || "ccc".equals(l1.get(i))) {
                System.out.println(i + "*************");
                l1.remove(i);
            }
            System.out.println(l1.get(i));
        }

        System.out.println("l1.size(): " + l1.size());
        System.out.println("结果：" + JSON.toJSONString(l1));



    }
}
